import {
  EventSubscriber,
  EntitySubscriberInterface,
  InsertEvent,
  UpdateEvent,
} from 'typeorm';
import { BaseEntity } from 'src/common/entities/base.entity';

@EventSubscriber()
export class BaseEntitySubscriber
  implements EntitySubscriberInterface<BaseEntity>
{
  listenTo() {
    return BaseEntity;
  }

  beforeInsert(event: InsertEvent<BaseEntity>): void {
    const entity = event.entity;
    entity.createTime = new Date();
    entity.updateTime = new Date();
    entity.createUser = 'admin';
    entity.updateUser = 'admin';
  }

  beforeUpdate(event: UpdateEvent<BaseEntity>): void {
    const entity = event.entity;
    entity.updateTime = new Date();
    entity.updateUser = 'admin';
  }
}
